package com.cancan.resultreturn.result1;


import com.cancan.resultreturn.result1.enums.ResponseCodeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.slf4j.MDC;

import java.io.Serializable;

/**
 * 基本响应父类
 * @author cancan
 */
@Data
public class CommonResponse<T> implements Serializable {

    private static final long serialVersionUID = -3214885479484534221L;
    private static final String MDC_KEY = "traceId";
    /**
     * 返回业务数据
     */
    private T data;

    /**
     * 响应编码
     */
    @ApiModelProperty(value="响应编码")
    private String code;

    /**
     * 响应描述
     */
    @ApiModelProperty(value="响应描述")
    private String message;

    /**
     * 请求ID
     */
    @ApiModelProperty(value="请求ID")
    private String traceId= MDC.get(MDC_KEY);

    /**
     * 响应时间戳
     */
    @ApiModelProperty(value="响应时间戳")
    private long timeStamp=System.currentTimeMillis();

    public CommonResponse(ResponseCodeEnum responseCodeEnum){
        this.code = responseCodeEnum.getCode();
        this.message = responseCodeEnum.getMessage();
    }

    public CommonResponse(String code, String message){
        this.code = code;
        this.message = message;
    }

    public CommonResponse(ResponseCodeEnum codeEnum, String message){
        this.code = codeEnum.getCode();
        this.message = message;
    }

    public CommonResponse(ResponseCodeEnum codeEnum, String message, T data){
        this.code = codeEnum.getCode();
        this.message = message;
        this.data = data;
    }

    public CommonResponse(T data){
        this.code = ResponseCodeEnum.SUCCESS.getCode();
        this.message = ResponseCodeEnum.SUCCESS.getMessage();
        this.data = data;
    }

    public CommonResponse() {
    }


    /**
     * 默认成功返回带参数
     * @param data
     * @return
     */
    public static  <T> CommonResponse<T> responseSuccess(T data) {
        CommonResponse<T> restResult = new CommonResponse<>(ResponseCodeEnum.SUCCESS);
        restResult.setData(data);
        return restResult;
    }

    /**
     * 默认成功返回
     * @return
     */
    public static <T> CommonResponse<T> responseSuccess() {
        CommonResponse<T> restResult = new CommonResponse<>(ResponseCodeEnum.SUCCESS);
        return restResult;
    }

    /**
     * 默认失败返回带参数
     * @param data
     * @return
     */
    public static <T> CommonResponse<T> responseFailed(String code, String message, T data) {
        CommonResponse<T> restResult = new CommonResponse<>(code, message);
        restResult.setData(data);
        return restResult;
    }

    /**
     * 默认失败返回
     * @return
     */
    public static <T> CommonResponse<T> responseFailed(String code, String message) {
        CommonResponse<T> restResult = new CommonResponse<>(code, message);
        return restResult;
    }

    /**
     * 默认失败返回
     * @return
     */
    public static <T> CommonResponse<T> responseFailed(ResponseCodeEnum responseCodeEnum) {
        CommonResponse<T> restResult = new CommonResponse<>(responseCodeEnum);
        return restResult;
    }

    /**
     * 默认失败返回
     * @return
     */
    public static <T> CommonResponse<T> responseFailed(ResponseCodeEnum responseCodeEnum, String message) {
        CommonResponse<T> restResult = new CommonResponse<>(responseCodeEnum, message);
        return restResult;
    }
}
